//
//  WNStatusViewModel.swift
//  XLWeibo
//
//  Created by 风不会停息 on 2017/7/27.
//  Copyright © 2017年 风不会停息. All rights reserved.
//

import UIKit

//微博cell的数据业务逻辑

class WNStatusViewModel: NSObject {
    
    var status : WNStatus?{
        didSet{
            handleMbrankImage(mbrank: status?.user?.mbrank ?? 0)
            
            //  处理认证类型等级图片
            handleVerifiedTypeImage(verifiedType: status?.user?.verified_type ?? -1)
            if let currentStatus = status {
                //  处理转发微博内容
                handleRetweetContent(status: currentStatus)
            }
            
            handleSourceContent(source: status?.source ?? "")
        }
    }
    
    var mbrankImage : UIImage?

    var verifiedTypeImage: UIImage?

    var retweetContent: String?
    
    //来源
    var sourceContent : String?
    
    
    var timeContent : String?{
        
        guard let createAt = status?.created_at else {
            
            return nil
        }
        let dt = DateFormatter()
        
        //  代码执行到此，时间对象一定有值
        if isThisYear(createAt: createAt) {
            //  是今年
            //  创建日历对象
            let calendar = Calendar.current
            //  是否是今天
            if calendar.isDateInToday(createAt) {
                //  今天
                //  获取时间差对应的秒数, 发微博时间距离当前时间差多少秒
                let timeInterval = abs(createAt.timeIntervalSinceNow)
                //  判断是否是1分钟之前
                if timeInterval < 60 {
                    return "刚刚"
                } else if timeInterval < 3600 {
                    //  判断是否是1小时之前的逻辑
                    let result = timeInterval / 60
                    return "\(Int(result))分钟前"
                } else {
                    //  其它时间
                    let result = timeInterval / 3600
                    return "\(Int(result))小时前"
                }
                
            } else if calendar.isDateInYesterday(createAt) {
                //  昨天
                dt.dateFormat = "昨天 HH:mm"
            } else {
                //  其它
                dt.dateFormat = "MM-dd HH:mm"
            }
        } else {
            //  不是今年
            dt.dateFormat = "yyyy-MM-dd HH:mm"
        }
        return dt.string(from: createAt)
    }
    
    private func isThisYear(createAt : Date) -> Bool {
        
        let dt  = DateFormatter()
        
        dt.dateFormat = "yyyy"
        
        let createAtYear = dt.string(from: createAt)
        
        let currentYear = dt.string(from: Date())
        
        return createAtYear == currentYear
    }

    //  处理转发微博内容
    private func handleRetweetContent(status: WNStatus) {
        if let retweetStatus = status.retweeted_status  {
            //  有转发微博内容
            //  获取转发微博用户昵称
            if let screenName = retweetStatus.user?.screen_name, let text = retweetStatus.text {
                //  昵称和转发微博内容一定有值，进行字符串的拼接
                retweetContent = "@\(screenName): \(text)"
            }
            
        }
        
    }
    
    
    //处理来源数据
    private func handleSourceContent(source : String){
        
        if let startRange = source.range(of: "\">") , let endRange = source.range(of: "</"){
            
            //upperBound 指定范围内的最大光标索引
            let startIndex = startRange.upperBound
            
            let endIndex = endRange.lowerBound
            
            let subStr = source.substring(with: startIndex..<endIndex)
            
            sourceContent = "来自" + subStr
            
        }
    }
    
    
    //  处理认证类型等级图片 (verifiedType 数字对应的图片)
    private func handleVerifiedTypeImage(verifiedType: Int) {
        switch verifiedType {
        case 0:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
    }


    //  处理会员等级图片（mbrank 对应的图片）
    private func handleMbrankImage(mbrank: Int64) {
        if mbrank >= 1 && mbrank <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        } else {
            mbrankImage = UIImage(named: "common_icon_membership_expired)")
        }
    }

}
